#include <bits/stdc++.h>
#define ll long long
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define P pair<ll,ll>
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
#define all(x) (x).begin(),(x).end()
using namespace std;

bool ok(ll m,ll n,string s){
    ll st=m;
    rep(i,s.size()){
        st=st-(ll)(s[s.size()-1-i]-'0');
        if(st<0)return false;
        st=st/n;
    }
    return true;
}

int main(){
    string x;
    ll m;
    cin>>x>>m;
    if(x.size()==1){
        ll num=x[0]-'0';
        if(num<=m)cout<<1<<endl;
        else cout<<0<<endl;
        return 0;
    }
    ll maxi=0;
    rep(i,x.size())maxi=max(maxi,(ll)(x[i]-'0'));
    ll l=1;
    ll r=LLONG_MAX;
    rep(i,100){
        ll mid=l+(r-l)/2;
        if(ok(m,mid,x))l=mid;
        else r=mid;
    }
    ll ans=max(0ll,l-maxi);
    cout<<ans<<endl;
}
